package com.taojie.graduation.utils;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class TreeUtils {
    private static List<Tree> menuCommon;
    public static List<Object> list = new ArrayList<Object>();

    public static List<Object> parentList(List<Tree> trees){
        menuCommon = trees;
        trees.stream()
                .parallel()
                .filter(x -> x.getParentId() == 0).forEach(x -> {
            HashMap<String, Object> mapArr = new HashMap<>();
            mapArr.put("obj", x.getObject());
            mapArr.put("child", childrenList(x.getId()));
            list.add(mapArr);
        });
        return list;
    }

    private static List<?> childrenList(int id){
        ArrayList<Object> list = new ArrayList<>();
        if (menuCommon.size() > 0){
            menuCommon.stream().parallel().filter(x -> x.getParentId() == id).forEach(x -> {
                HashMap<String, Object> childArr = new HashMap<>();
                childArr.put("obj", x.getObject());
                childArr.put("child", childrenList(x.getId()));
                list.add(childArr);
            });
        }
        return list;
    }

    public static List<Object> distinct(List<Tree> menu){
        ArrayList<Object> objects = new ArrayList<>(parentList(menu));
        menuCommon.clear();
        list.clear();
        return objects;
    }

}
